import { MemoryManager } from "GameLoop/DecisionMaking/MemoryManager";
import { cpu_used } from "utils/decorate/decorate";
 

export class GlobalStateUpdate 
{
    @cpu_used()
    public static checkExpireCreeps(worldgetter:WorldGetter,  emitter:TEventEmitter<TEventTypes>)
    {
        //let existcreep:Record<string,boolean>={};
        for(let k in Game.rooms)
        {
            (Game.rooms[k] as any)._cacheCreep={};
        }
        const deadCreep:string[]=[];
        const healEventRoom:Set<string>=new Set<string>();
        for(const k in Memory.creeps)
        {
            if(!Game.creeps[k])
            {
                deadCreep.push(k);
               
                continue;
            }
            // if(StationNetwork.getInstance.PathFinder._blockCreeps[k])
            // {
            //     existcreep[k]=true;
            // }
            if(Game.creeps[k].hits<Game.creeps[k].hitsMax && Game.creeps[k].ticksToLive!>200)
            {
                Game.creeps[k].room.registerHurtCreep(Game.creeps[k]);
                // 抛出治疗事件
                if(Game.creeps[k].room.getType() ==1)
                {
                    // emitter(Game.creeps[k].room.name,"HealCreep",{
                    //     region:Game.creeps[k].room.lordRoom().name,
                    //     room:Game.creeps[k].room.name,
                    // })
                    healEventRoom.add(Game.creeps[k].room.name);
                }
               
            }
            Game.creeps[k].initAttributes();

        }
        // 2.更新每个房间的爬虫信息
        // StationNetwork.getInstance.PathFinder._blockCreeps=existcreep;
        healEventRoom.forEach(e=>
            {
                emitter(e,"HealCreep",{
                        region:Game.rooms[e].lordRoom().name,
                        room:e,
                    })
            })
        deadCreep.forEach(k=>
        {
            
            MemoryManager.deleteNodeHeap(k);
            delete Memory.creeps[k];
        })
    }
   


    public static checkUpdateMarket(worldgetter:WorldGetter,  emitter:TEventEmitter<TEventTypes>)
    {
        global.market.Update();
    }
}